package com.ck567.netty.chatroom.server.handler

import com.ck567.netty.chatroom.message.GroupMembersRequestMessage
import com.ck567.netty.chatroom.message.GroupMembersResponseMessage
import com.ck567.netty.chatroom.server.session.GroupSessionFactory
import com.ck567.netty.chatroom.util.logger
import io.netty.channel.ChannelHandler
import io.netty.channel.ChannelHandlerContext
import io.netty.channel.SimpleChannelInboundHandler

@ChannelHandler.Sharable
class GroupMembersMessageHandler : SimpleChannelInboundHandler<GroupMembersRequestMessage>() {
    override fun channelRead0(ctx: ChannelHandlerContext, msg: GroupMembersRequestMessage) {
        logger.info("================")
        logger.info("查询群组成员：$msg")
        val members = GroupSessionFactory.getSession().getMembers(msg.groupName)
        if(members!=null && members.isNotEmpty()) {
            ctx.writeAndFlush(GroupMembersResponseMessage(true, "", members))
        }else{
            ctx.writeAndFlush(GroupMembersResponseMessage(false, "未查询到群组信息！", emptySet()))
        }
    }
    companion object {
        private val logger = logger<GroupMembersMessageHandler>()
    }
}